<template>
  <div class="box">
    <Loading v-if="!proDetailData.type" />
    <div v-else>
      <van-nav-bar title="确认订单" fixed @click-left="onClickLeft" left-arrow></van-nav-bar>
      <div class="order_box">
        <div class="order_pro">
          <div class="order_pro_top">
            <img :src="proDetailData.pro_pic" alt="">
            <div class="order_pro_text">
              <p>{{proDetailData.pro_introduce}}</p>
              <span>{{'￥' + proDetailData.pro_price}}</span>
              <van-stepper class="stepper" v-model="num" integer />
            </div>
          </div>
          <div class="order_pro_bottom">
            <span>小计 :</span>
            <span>{{'￥' + proDetailData.pro_price * num}}</span>
            <b v-if="preferentialPrice === 0"></b>
            <b v-else>{{'已优惠￥' + preferentialPrice}}</b>
          </div>
        </div>
        <div class="order_tel">
          <p>
            <span>订单备注 :</span>
            <input type="text" maxlength="50" :value="order_remarks" placeholder="选填,请先和商家协商一致" @change="changeRemarks">
          </p>
          <p>
            <span>手机号 :</span>
            <span>{{username}}</span>
          </p>
        </div>
        <p class="order_hint1">*未满18周岁的手术人需在监护人陪同下到院面诊</p>
        <p class="order_hint2">*您本次购买的商品不需要收货地址，请仔细确认您的手机号</p>
      </div>
      <div class="order_buy">
        <div>
          <span v-if="proDetailData">{{'￥' + proDetailData.pro_price * num}}</span>
          <span @click="goOrder(proDetailData.proid)">确认订单</span>
        </div>
        <img src="@/assets/images/icon_kefu.png" alt="">
      </div>
    </div>
  </div>
</template>

<script>
import Loading from '../../components/loading'
import { getProductById, submitOrder, getWelfareProDetailList } from '../../api'
import Vue from 'vue'
import { NavBar, Stepper, Toast } from 'vant'
Vue.use(NavBar)
Vue.use(Stepper)
Vue.use(Toast)
export default {
  data () {
    return {
      proid: '',
      proDetailData: [],
      num: 1,
      order_remarks: '',
      preferentialPrice: 0,
      username: localStorage.getItem('userTel_AM'),
      loginState: localStorage.getItem('loginState_AM')
    }
  },
  components: {
    Loading
  },
  computed: {},
  mounted () {
    this.proid = this.$route.query.proid
    this.preferentialPrice = this.$route.query.preferentialPrice
    this.getProDetail(this.proid)
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    getProDetail (proid) {
      if (proid.substring(1, 2) === '0') {
        getProductById({
          proid: proid,
          username: localStorage.getItem('userTel_AM')
        }).then(res => {
          if (res.data.code === '200') {
            this.proDetailData = res.data.data[0]
          }
        })
      } else {
        getWelfareProDetailList({ proid: proid }).then(res => {
          this.proDetailData = res.data.data[0]
        })
      }
    },
    changeRemarks (e) {
      this.order_remarks = e.target.value
    },
    goOrder (proid) {
      const loginState = localStorage.getItem('loginState_AM')
      const username = localStorage.getItem('userTel_AM')
      const token = localStorage.getItem('token_AM')
      if (username && token && loginState === 'ok') {
        submitOrder({
          username: localStorage.getItem('userTel_AM'),
          token: localStorage.getItem('token_AM'),
          proid,
          pro_num: this.num,
          pro_price: this.proDetailData.pro_price * this.num - this.preferentialPrice,
          pro_coupons: this.preferentialPrice,
          pro_remark: this.order_remarks,
          status: '0'
        }).then(res => {
          if (res.data.code === '200') {
            Toast('订单添加成功')
            setTimeout(() => {
              this.$router.push({ path: '/payOrder', query: { order_id: res.data.data.order_id } })
            }, 1000)
          } else if (res.data.code === '400') {
            Toast('您的身份认证已过期，请重新登录')
            setTimeout(() => {
              this.$router.push('/login')
            }, 700)
          }
        })
      } else {
        Toast('您还没有登录，快去登录吧')
        setTimeout(() => {
          this.$router.push('/login')
        }, 300)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.order_box {
  width: 100%;
  height: auto;
  margin-top: 0.46rem;
  .order_pro {
    width: 100%;
    height: 1.82rem;
    padding: 0.22rem 0.3rem 0.16rem 0.3rem;
    box-shadow: 0rem 0.04rem 0.05rem 0px rgba(97,97,97,0.2);
    border-radius: 0.06rem;
    .order_pro_top {
      width: 100%;
      height: 0.85rem;
      img {
        width: 0.75rem;
        height: 0.75rem;
        float: left;
        margin-right: 0.15rem;
      }
      .order_pro_text {
        width: 2.25rem;
        height: 0.85rem;
        float: left;
        p {
          width: 100%;
          height: 0.32rem;
          line-height: 0.16rem;
          font-size: 0.14rem;
          margin-bottom: 0.18rem;
          color: #464646;
          overflow : hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        span {
          font-size: 0.16rem;
          line-height: 0.35rem;
          color: #FF5959;
          float: left;
        }
        .stepper {
          float: right;
        }
      }
    }
    .order_pro_bottom {
      width: 100%;
      height: 0.59rem;
      padding: 0.43rem 0 0 0;
      line-height: 0.16rem;
      font-size: 0.14rem;
      span:nth-of-type(1) {
        width: 0.65rem;
        float: left;
        text-align: right;
        margin-right: 0.26rem;
        color: #666666;
      }
      span:nth-of-type(2) {
        max-width: 0.5rem;
        float: left;
        color: #464646;
        text-align: left;
      }
      b {
        font-weight: normal;
        float: right;
        color: #ff5959;
        font-size: 0.12rem;
      }
    }
  }
  .order_tel {
    width: 100%;
    height: 0.85rem;
    margin-top: 0.15rem;
    box-shadow: 0 0.04rem 0.05rem 0 rgba(97,97,97,0.2);
    padding: 0.18rem 0.3rem;
    border-radius: 0.06rem;
    p:nth-of-type(1) {
      margin-bottom: 0.18rem;
    }
    p {
      line-height: 0.16rem;
      font-size: 0.14rem;
      span:nth-of-type(1) {
        width: 0.65rem;
        float: left;
        text-align: right;
        margin-right: 0.26rem;
        color: #666666;
      }
      span:nth-of-type(2) {
        float: left;
      }
      input {
        width: 2.2rem;
        border: none;
        height: 0.16rem;
      }
    }
  }
  .order_hint1 {
    margin-top: 0.3rem;
    padding-left: 0.3rem;
    width: 100%;
    height: 0.14rem;
    line-height: 0.14rem;
    font-size: 0.12rem;
    color: #a0a0a0;
  }
  .order_hint2 {
    margin-top: 0.15rem;
    padding-left: 0.3rem;
    width: 100%;
    height: 0.14rem;
    line-height: 0.14rem;
    font-size: 0.12rem;
    color: #a0a0a0;
  }
}
.order_buy {
  width: 100%;
  height: 0.5rem;
  padding: 0 0.15rem;
  position: fixed;
  bottom: 0;
  background: white;
  div {
    width: 2.3rem;
    height: 0.36rem;
    margin: 0.07rem 0;
    span {
      width: 50%;
      height: 100%;
      float: left;
      font-size: 0.18rem;
      line-height: 0.36rem;
      text-align: center;
    }
    span:nth-of-type(1) {
      background: #FFE7E8;
      border-radius: 0.18rem 0 0 0.18rem;
      color: #FF5959;
    }
    span:nth-of-type(2) {
      background: #FF5959;
      border-radius: 0 0.18rem 0.18rem 0;
      color: #FFFFFF;
    }
  }
  img {
    width: 0.55rem;
    height: 0.6rem;
    float: left;
    position: absolute;
    right: 0.25rem;
    bottom: 0.1rem;
  }
}
/deep/ .van-icon-arrow-left {
  font-size: 0.22rem;
  color: #171717;
}
/deep/ .van-nav-bar__title {
  font-size: 0.18rem;
  color: #171717;
  font-weight: bold;
}
/deep/ .van-nav-bar {
  z-index: 1000;
}
/deep/ .van-stepper__minus--disabled {
  width: 0.34rem;
  height: 0.34rem;
  color: #cecece!important;
}
/deep/ .van-stepper__input {
  width: 0.34rem;
  height: 0.34rem;
  color: #464646;
}
/deep/ .van-stepper__plus {
  width: 0.34rem;
  height: 0.34rem;
  color: #ff5959;
}
/deep/ .van-stepper__minus {
  width: 0.34rem;
  height: 0.34rem;
  color: #ff5959;
}
</style>
